package com.wd.myhome.view.fragment
import android.content.Intent
import android.provider.ContactsContract.Data
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageView
import android.widget.ListAdapter
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import coil.load
import coil.transform.RoundedCornersTransformation
import com.ayvytr.adapter.smart
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.wd.base.App
import com.wd.current.model.Bean.DataBean
import com.wd.live.base.BaseFragment
import com.wd.myhome.R
import com.wd.myhome.databinding.FragmentHomeBinding
import com.wd.myhome.view.activity.Constellation
import com.wd.myhome.view.activity.LiveRoomActivity
import com.wd.myhome.viewmodel.HomeViewModel
class HomeFragment : BaseFragment<HomeViewModel,FragmentHomeBinding>(R.layout.fragment_home) {
    var type=100
    var page=1
    var ListAdapte=
        smart(mutableListOf<DataBean.HomeList.Result>(),R.layout.item_home,{
            item, position ->
            findViewById<ImageView>(R.id.img).load(item.liveCoverUrl)
            findViewById<TextView>(R.id.text_name).text=item.nickName
            findViewById<TextView>(R.id.text_title).text = "${item.liveTitle}"
            if (item.liveStatus == 1) {
                findViewById<TextView>(R.id.text_state).text = "直播中"
            } else if (item.liveStatus == 2) {
                findViewById<TextView>(R.id.text_state).text = "回放"
            }
        }){}.click { item, position ->
            //点击列表条目，跳转到观看页面
            startActivity(Intent(activity,LiveRoomActivity::class.java).apply {
                putExtra("liveStatus",item.liveStatus)
                putExtra("liveId",item.liveId)
                putExtra("liveTitle",item.liveTitle)
                putExtra("liveCoverUrl",item.liveCoverUrl)
                putExtra("headPic",item.headPic)
                putExtra("nickName",item.nickName)
            })
        }
    override fun dataObserver() {
        viewmodel.liveListBean.observe(this, {
            ListAdapte.update(it.result)
        })
    }

    override fun initViews() {
        binding.zi.setOnClickListener {
//            Intent intent = new Intent(this.context,Constellation.class)
            startActivity(Intent(this.context, Constellation::class.java))
        }
        binding.tab.addTab(binding.tab.newTab().setText("关注"))
        binding.tab.addTab(binding.tab.newTab().setText("校园"))
        binding.tab.addTab(binding.tab.newTab().setText("美食"))
        binding.tab.addTab(binding.tab.newTab().setText("健身"))
        binding.tab.addTab(binding.tab.newTab().setText("跳舞"))
        binding.tab.addTab(binding.tab.newTab().setText("音乐"))
        viewmodel.getList(App.userId,type,page,10)
        binding.recy.adapter=ListAdapte
        binding.recy.layoutManager=GridLayoutManager(requireContext(),2)
        binding.tab.addOnTabSelectedListener(object :OnTabSelectedListener{
            override fun onTabSelected(tab: TabLayout.Tab?) {
                when(tab?.position){
                    0->{
                        viewmodel.getList(App.userId,100,page,10)
                        binding.recy.adapter=ListAdapte
                        binding.recy.layoutManager=GridLayoutManager(requireContext(),2)
                        type=100
                    }
                    1->{
                        viewmodel.getList(App.userId,type,page,10)

                        type=101
                    }
                    2->{
                        viewmodel.getList(App.userId,type,page,10)

                        type=102
                    }
                    3->{
                        viewmodel.getList(App.userId,type,page,10)

                        type=103
                    }
                    4->{

                        viewmodel.getList(App.userId,type,page,10)
                        type=104

                    }
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })

        binding.sm.setRefreshHeader(ClassicsHeader(requireContext()))
        binding.sm.setRefreshFooter(ClassicsFooter(requireContext()))
        binding.sm.setOnRefreshListener {
            ListAdapte.clear()
            page=1
            viewmodel.getList(App.userId,type,page,10)
            binding.sm.finishRefresh()
        }
        binding.sm.setOnLoadMoreListener {
            page++
            viewmodel.getList(App.userId,type,page,10)
            binding.sm.finishLoadMore()
        }
        viewmodel.getList(App.userId,type,page,10)
        binding.recy.adapter=ListAdapte
        binding.recy.layoutManager=GridLayoutManager(requireContext(),2)

        //点击跳转星座匹配页面
        binding.zi.setOnClickListener(object :OnClickListener{
            override fun onClick(p0:View?){
                startActivity(Intent(activity,Constellation::class.java))
            }
        })

    }

}